package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemTransferCheckBinding;
import com.jinlejia.erp.ui.mine.transfer.TransferCheckActivity;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/7 0007.
 */

public class TransferCheckAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;
    private int checkId = -1;//被选中的条目id，-1为均未选中

    public TransferCheckAdapter(ArrayList<Map<String, Object>> data, Context context) {
        super(data);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
//        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_transfer_check, parent, false);
        return new ViewHolder(view);
    }

    public void setCheckId(int id) {
        this.checkId = id;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ItemTransferCheckBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final Map<String, Object> map = mData.get(position);
        binding.tvName.setText(map.get("name").toString());

        if (checkId != -1) {
            if (position == checkId) {
                binding.imgCheck.setImageResource(R.mipmap.ic_btn_check);
            } else {
                binding.imgCheck.setImageResource(R.mipmap.ic_btn_default);
            }
        } else {
            binding.imgCheck.setImageResource(R.mipmap.ic_btn_default);
        }

        binding.llLayout.setOnClickListener(view -> {
            setCheckId(position);
            ((TransferCheckActivity) context).setResultData(position);
        });
        binding.getRoot().setTag(mData.get(position));
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }
}
